﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.Serialization;
using Convertor.Interface;
using Wpf.CommonTools.StringExtension;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.SendEmail
{
    [Serializable]
    public class ConfigContent : IConfigContent, ISerializable, ICloneable, INotifyPropertyChanged
    {
        private ObservableCollection<SmtpClientViewModel> smtpClientList = new ObservableCollection<SmtpClientViewModel>();

        public ObservableCollection<SmtpClientViewModel> SmtpClientList
        {
            get { return smtpClientList; }
            set { smtpClientList = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }

        private bool isHtml = true;

        public bool IsHtml
        {
            get { return isHtml; }
            set { isHtml = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private string body;

        public string Body
        {
            get { return body; }
            set { body = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private string subject;

        public string Subject
        {
            get { return subject; }
            set { subject = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }

        public ConfigContent()
        {
        }

        public override string ToString()
        {
            return ToolTip;
        }

        #region IToolTip 成员

        public string ToolTip
        {
            get
            {
                return string.Format(UILanguage.TryFindResource("Plugin_SendEmail_Info"),
                    this.Subject.ToLimitLength(100),
                    this.smtpClientList.Count);
            }
        }

        #endregion

        #region ISerializable 成员

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("SmtpClientList", SmtpClientList, typeof(ObservableCollection<SmtpClientViewModel>));
            info.AddValue("Body", Body, typeof(string));
            info.AddValue("Subject", Subject, typeof(string));
            info.AddValue("IsHtml", IsHtml, typeof(bool));
        }

        public ConfigContent(SerializationInfo info, StreamingContext context)
        {
            smtpClientList = (ObservableCollection<SmtpClientViewModel>)info.GetValue("SmtpClientList", typeof(ObservableCollection<SmtpClientViewModel>));
            body = (string)info.GetValue("Body", typeof(string));
            subject = (string)info.GetValue("Subject", typeof(string));
            isHtml = (bool)info.GetValue("IsHtml", typeof(bool));
        }

        #endregion

        #region ICloneable 成员

        public object Clone()
        {
            ConfigContent content = new ConfigContent()
            {
                SmtpClientList = this.SmtpClientList,
                IsHtml = this.IsHtml,
                Body = this.Body,
                Subject = this.Subject
            };
            return content;
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
